import { FC, useState } from 'react';
import MoreHandleMenu from '@/page-compoennt/miner/moveToGroupAndMoreHandle/MoreHandleMenu';
import { Dropdown, message } from 'antd';
import { MenuInfo } from 'rc-menu/es/interface';
import { history, useLocation } from 'umi';
import TaskModal from '@/page-compoennt/miner/moveToGroupAndMoreHandle/TaskModal';
import MinerLogModal from '@/page-compoennt/miner/moveToGroupAndMoreHandle/MinerLogModal';

interface IMoreHandleDropDownProps {
  miners: string[];
  mid: number;
  mutateMiner: () => void;
  banMinerDt?: boolean;
}

const MoreHandleDropDown: FC<IMoreHandleDropDownProps> = (props) => {
  const { miners, mid, mutateMiner, banMinerDt } = props;
  const [taskModalVisible, setTaskModalVisible] = useState(false);
  const [logModalVisible, setLogModalVisible] = useState(false);
  const [task, setTask] = useState(-1);
  const length = miners.length;

  const handleClick = (info: MenuInfo) => {
    if (info.key === 'miner-dt') {
      if (length > 1) {
        return message.info('该操作只支持单台矿机');
      }
      return history.push(`/miner-dt/${mid}/${miners[0]}`);
    }
    if (info.key === 'miner-log') {
      if (length > 1) {
        return message.info('该操作只支持单台矿机');
      }
      return setLogModalVisible(true);
    }

    setTask(parseInt(info.key));
    setTaskModalVisible(true);
  };
  return (
    <>
      <Dropdown
        trigger={['click']}
        overlay={
          <MoreHandleMenu banMinerDt={banMinerDt} onClick={handleClick} />
        }
        getPopupContainer={() =>
          document.getElementById('minerMoreCheckHandleWrap') || document.body
        }
        placement="topCenter"
      >
        {props.children}
      </Dropdown>
      <TaskModal
        visible={taskModalVisible}
        setVisible={setTaskModalVisible}
        task={task}
        mid={mid}
        miners={miners}
        mutateMiner={mutateMiner}
      />
      {logModalVisible ? (
        <MinerLogModal
          visible={logModalVisible}
          setVisible={setLogModalVisible}
          mid={mid}
          miners={miners}
        />
      ) : null}
    </>
  );
};

export default MoreHandleDropDown;
